﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlSugar操作数据库
{
    class DALDemo
    {

        //BookStoreDataSetTableAdapters.bookTableAdapter adapter
        //    = new BookStoreDataSetTableAdapters.bookTableAdapter();

        //adapter.Insert("C#语法入门",34.5,"叶静");

        //BookStoreDataSet.bookDataTable bookDataTable = adapter.GetData();
        //Console.WriteLine(bookDataTable.TableName);
        //Console.WriteLine(bookDataTable.nameColumn);
        //Console.WriteLine(bookDataTable.Count);

        //BookStoreDataSet.bookRow row0 = bookDataTable[0];
        ////row0.BeginEdit();
        //row0.name = "康熙大帝2";
        ////row0.EndEdit();
        //adapter.Update(row0);


        //for (int i = 0; i < bookDataTable.Count; i++)
        //{
        //    BookStoreDataSet.bookRow bookRow = bookDataTable[i];
        //    Console.WriteLine(bookRow.name);
        //}

        //adapter.Insert("C++入门学习", 45.6, "叶静");

        //BookStoreDataSet.bookDataTable book = adapter.GetData();
        //if (book.Count < 1)
        //{
        //    Console.WriteLine(0);
        //}
        //else
        //{
        //    BookStoreDataSet.bookRow bookRow1 = book[1];
        //    Console.WriteLine(bookRow1.name);
        //    Console.WriteLine(bookRow1.price);
        //    Console.WriteLine(bookRow1.author);
        //}

        //for(int i = 0; i < book.Count;i++)
        //{
        //    BookStoreDataSet.bookRow bookRow1 = book[i];
        //    Console.WriteLine(bookRow1.name);
        //    Console.WriteLine(bookRow1.price);
        //    Console.WriteLine(bookRow1.author);
        //}

        private BookStoreDataSetTableAdapters.bookTableAdapter GetBookAdapter()
        {
            return new BookStoreDataSetTableAdapters.bookTableAdapter();
        }

        public void ShowAllBooks()
        {
            BookStoreDataSetTableAdapters.bookTableAdapter adapter = GetBookAdapter();
            BookStoreDataSet.bookDataTable book = adapter.GetData();
            for (int i = 0; i < book.Count; i++)
            {
                BookStoreDataSet.bookRow bookRow = book[i];
                Console.WriteLine("{0},{1},{2}",bookRow.name,bookRow.price,bookRow.author);
            }
        }

        public void FindBooksByName(String name)
        {
            BookStoreDataSetTableAdapters.bookTableAdapter adapter = GetBookAdapter();
            BookStoreDataSet.bookDataTable book = adapter.GetData();
            var x = from a in book where a.name.Contains(name) select a;
            foreach (var c in x)
            {
                Console.WriteLine($"name: {c.name}, price: {c.price}, author: {c.author}");
                //c.price += 5;
                //adapter.Update(c);
            }
        }
    }
}
